package com.apiit.webserviceclient.utility;

import android.R.bool;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.apiit.webserviceclient.Caller;

public class BPEL_Activity extends Activity {

	public static String result = "";
	
	//public static String result;

	protected void fireEvent(String actionID,
			Class<?> successfulCls, Class<?> unSuccessfulCls, String... string) throws InterruptedException { 
		
		
		String appendenedParameters = "";

		for (int i = 0; i < string.length; i++) {
			appendenedParameters = appendenedParameters + string[i];
			if(i<string.length-1){
				
				appendenedParameters= appendenedParameters+"&";
			}
		}
		
		String soapRequest = this.getClass().getSimpleName() + "---" + actionID + "---"+ appendenedParameters;
		
		boolean haveAuthenticationDetails;//check the data base and fill this value
		//if(unSuccessfulCls != null && !haveAuthenticationDetails)
		{
			Intent i = new Intent(this, unSuccessfulCls);
			//i.putExtra("params", tempArray2[0]);
			startActivity(i);
		}
		//else
		{
		if(unSuccessfulCls != null)
		{
			String authenticationDetails = "";//(UsreName=sfsdf&Password=sdfsd)
		
			//retrieve key and value of username and password from the database 
			soapRequest = soapRequest + "&" + authenticationDetails;
		}
		result = "START";

		//-------------------
		String cookie = "";
		//-------------------
		soapRequest = soapRequest + "===cookie===" + cookie;
		Caller caller = new Caller();
		caller.parameters = soapRequest;
		caller.join();
		caller.start();

		while (result.equals("START")) {
			Thread.sleep(10);
		}
		
		String [] tempArray1 = result.split("===status===");
		String [] tempArray2;
		
		if (tempArray1[1].equalsIgnoreCase("SUCCESS")) {
			tempArray2 = tempArray1[0].split("===cookie===");
			if(unSuccessfulCls != null)
			{
				//insert cookies to db(tempArray2[1])
			}
			Intent i = new Intent(this, successfulCls);
			i.putExtra("params", tempArray2[0]);
			startActivity(i);
		}
		else
		{
			if(unSuccessfulCls != null)
			{
				Intent i = new Intent(this, unSuccessfulCls);
				//i.putExtra("params", tempArray2[0]);
				startActivity(i);
			}
			else
			{
				Toast.makeText(getApplicationContext(), "Error!!!", Toast.LENGTH_SHORT).show();
			}
		}
		}
	}

	protected void initialDisplayProcess() {
		
		Intent intename = getIntent();
		result= intename.getStringExtra("params");
		String [] finalResult = result.split("---");
		String [] keyValuePair;
		String [] keyDetails;
		
		for(int i=0; i<finalResult.length; i++)
		{
			//keyValuePair = finalResult[i].split("*");
			keyValuePair = finalResult[i].split("===");
			keyDetails = keyValuePair[0].split("_");
			if(keyDetails[0].equals("btn"))
			{
			//	Button button= (Button)findViewById(Integer.parseInt("0x7"+keyDetails[1]));
				Button button= (Button)findViewById(Integer.parseInt(keyDetails[1]));
				button.setText(keyValuePair[1]);
			}
			
			else if(keyDetails[0].equals("txtView"))
			{
				TextView txtView= (TextView)findViewById(Integer.parseInt(keyDetails[1]));
				txtView.setText(keyValuePair[1]);
			}
//			else if(keyDetails[0].equals("lbel"))
//			{
//				Labe txtView= (TextView)findViewById(Integer.parseInt(keyDetails[1]));
//				txtView.setText(keyValuePair[1]);
//			}
		}

	}

}
